package com.mars.admin.modules.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.mars.admin.common.group.AddGroup;
import com.mars.admin.common.group.UpdateGroup;
import com.mars.admin.common.request.activity.ActivityRequest;
import com.mars.admin.framework.common.Result;
import com.mars.admin.modules.activity.entity.Activity;
import com.mars.admin.modules.activity.service.IActivityService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 后台活动管理接口
 */
@Slf4j
@RestController
@RequestMapping("/admin/Activity")
@Tag(name = "后台活动管理接口", description = "后台活动管理接口")
@AllArgsConstructor
public class AdminActivityController {


    private final IActivityService activityService;


    /**
     * 新增活动
     */
    @PostMapping("/add")
    @Operation(summary = "新增活动", description = "新增活动")
    public Result<Void> add(@Validated(AddGroup.class) @RequestBody ActivityRequest request) {
        log.info("新增活动信息：{}", request);
        request.setUserId(StpUtil.getLoginIdAsLong());
        boolean result = activityService.publish(request);
        return result ? Result.success() : Result.error("新增活动失败");
    }


    /**
     * 更新活动
     */
    @PostMapping("/update")
    @Operation(summary = "更新活动", description = "更新活动")
    public Result<Void> update(@Validated(UpdateGroup.class) @RequestBody ActivityRequest request) {
        log.info("更新活动信息：{}", request);
        boolean result = activityService.publish(request);
        return result ? Result.success() : Result.error("更新活动失败");
    }


    /**
     * 分页查询活动
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询活动", description = "分页查询活动")
    public Result<Page<Activity>> page(@RequestBody ActivityRequest request) {
        Page<Activity> page = Page.of(request.getPageNumber(), request.getPageSize());
        Page<Activity> result = activityService.pageActivity(page, request);
        return Result.success(result);
    }

    /**
     * 获取分类
     */
    @GetMapping("/get/{id}")
    @Operation(summary = "获取活动", description = "获取活动")
    public Result<Activity> get(@PathVariable Long id) {
        log.info("根据ID获取活动：{}", id);
        return Result.success(activityService.getById(id));
    }

    /**
     * 删除活动
     */
    @PostMapping("/delete/{id}")
    @Operation(summary = "删除活动", description = "删除活动")
    public Result<Void> delete(@PathVariable Long id) {
        log.info("根据ID删除活动：{}", id);
        boolean result = activityService.deleteById(id);
        return result ? Result.success(): Result.error("删除活动失败");
    }


    /**
     * 审核活动
     */
    @PostMapping("/audit/{id}")
    @Operation(summary = "审核活动", description = "审核活动")
    @Parameter(name = "id", description = "活动ID", required = true)
    @SaCheckLogin
    public Result<Void> auditActivity(@PathVariable Long id, @RequestParam Integer auditStatus, @RequestParam(required = false) String auditReason) {
        Long auditUserId = StpUtil.getLoginIdAsLong();
        activityService.auditActivity(id, auditStatus, auditReason, auditUserId);
        return Result.success();
    }
}
